// 获取城市api
// 经纬度：116.40528,39.90498 | 城市命：北京市 | LocationID: 101010100
export const getCityInfo = async (coords: string) => {
  const key = '5653c0b010854c9a9778c3174186494d';
  const url = `https://geoapi.qweather.com/v2/city/lookup?location=${coords}&key=${key}`;
  try {
    const ret = await (await fetch(url, {method: 'GET'})).json();
    console.log('城市信息', ret);
    if (ret.code === '200') {
      return ret.location[0];
    } else {
      return [];
    }
  } catch (error) {
    console.log('error', error);
  }
};

// 获取3天的天气预报
export const getThreeDays = async (coords: string) => {
  const key = '5653c0b010854c9a9778c3174186494d';
  const url = `https://devapi.qweather.com/v7/weather/3d?location=${coords}&key=${key}`;
  try {
    const ret = await (await fetch(url, {method: 'GET'})).json();
    console.log('3天的天气预报', ret);
    if (ret.code === '200') {
      return ret.daily;
    } else {
      return [];
    }
  } catch (error) {
    console.log('error', error);
  }
};

// 获取生活指数
export const getIndices = async (coords: string, type: string) => {
  const key = '5653c0b010854c9a9778c3174186494d';
  const url = `https://devapi.qweather.com/v7/indices/1d?location=${coords}&key=${key}&type=${type}`;
  try {
    const ret = await (await fetch(url, {method: 'GET'})).json();
    console.log('获取生活指数', ret);
    if (ret.code === '200') {
      return ret.daily;
    } else {
      return {};
    }
  } catch (error) {
    console.log('error', error);
  }
};

// 获取聚合新闻
export const getNewsList = async (type: string) => {
  const key = '964bdd276fbbdc1465ada62f55325915';
  const url = `http://v.juhe.cn/toutiao/index?type=${type}&key=${key}`;
  try {
    const ret = await (await fetch(url, {method: 'GET'})).json();
    console.log('新闻信息', ret);
    if (ret.error_code === 0) {
      return ret.result.data;
    } else {
      return [];
    }
  } catch (error) {
    console.log('error', error);
  }
};
